<?php if (!defined('BASEPATH')) die();
class Frontpage extends Front_End_Controller
{

   public function index(){      
      $this->load->view('include/header',$this->getHeaderData());
      $this->load->view('front_end/header_frontend',array('page'=> 'home'));
      $this->load->view('front_end/home');
      $this->load->view('front_end/footer_frontend');
      $this->load->view('include/footer');
   }
   
    public function quienesSomos(){
      $this->load->view('include/header',$this->getHeaderData());
      $this->load->view('front_end/header_frontend',array('page'=> __FUNCTION__));
      $this->load->view('front_end/quienesSomos');
      $this->load->view('front_end/footer_frontend');
      $this->load->view('include/footer');
   }
   
    public function comoFunciona(){
      $this->load->view('include/header',$this->getHeaderData());
      $this->load->view('front_end/header_frontend',array('page'=> __FUNCTION__));
      $this->load->view('front_end/funcionamiento');
      $this->load->view('front_end/footer_frontend');
      $this->load->view('include/footer');
   }
   
    public function precios(){
      $this->load->view('include/header',$this->getHeaderData());
      $this->load->view('front_end/header_frontend',array('page'=> __FUNCTION__));
      $this->load->view('front_end/precios');
      $this->load->view('front_end/footer_frontend');
      $this->load->view('include/footer');
   }
   
    public function pediTuClase() {
        $data = array();
        if (!empty($_POST)) {
            $this->load->helper(array('form', 'url'));
            $this->load->library('form_validation');
            $this->form_validation->set_rules('mail', 'Email', 'required|valid_email');            
            if ($this->form_validation->run() == true) {
                $id = $this->nuevoPedido();
                $data = $this->sendEmail($id);
                
            }else{
                $data['msg'] = validation_errors();
                $data['operation_result'] = 'danger';
            }
        }

        $this->load->model('materiaModel');
        $this->load->model('barrioModel');        
        $this->load->model('partidoModel');
        $data['materias'] = $this->materiaModel->getWithProfesorByModel(1);
        $data['niveles'] = $this->materiaModel->getLevels();
        $data['partidos'] = $this->partidoModel->getAll();
        $data['barrios'] = $this->barrioModel->getAll();
        $this->load->view('include/header', $this->getHeaderData());
        $this->load->view('front_end/header_frontend', array('page' => __FUNCTION__));
        $this->load->view('front_end/pedidoClase', $data);
        $this->load->view('front_end/footer_frontend');
        $this->load->view('include/footer');
    }
    
    public function materiasByNivel(){
        $levelId = $this->input->post('nivelId');
        $this->load->model('materiaModel');        
        echo json_encode($this->materiaModel->getByLevel($levelId));
    }
    
    private function nuevoPedido(){
        $this->load->model('alumnoModel');
        $this->load->model('pedidoModel');
        $exist = $this->alumnoModel->exist($this->input->post('mail'));
        if($this->input->post('distrito') == 'cf'){
            $barrio = $this->input->post('barrio');
            $partido = null;
        }else{
            $barrio = null;
            $partido = $this->input->post('localidad');
            
        }
        if($exist->num_rows()){
            $alumno = $exist->result();
            $this->alumnoModel->update($alumno[0]->id,$this->input->post('name'),$this->input->post('mail'),$this->input->post('telefono'),
                    (int)$partido,(int)$barrio);
            
            
            $idAlumno =$alumno[0]->id;
        }else{
           $idAlumno = $this->alumnoModel->create($this->input->post('name'),$this->input->post('mail'),$this->input->post('telefono'),
            (int)$partido,(int)$barrio);
           
        }
        return $this->pedidoModel->create($idAlumno,$this->input->post('comentario'),
                   $this->input->post('materia'),$this->input->post('lugar'));
    }
    
    private function sendEmail($id){
        $data = array();
        if($this->input->post('lugar') == 'casa' && $this->input->post('barrio') == ''){
                    $data['msg'] = 'Por favor ingresa tu barrio';
                    $data['operation_result'] = 'danger';
                }else{
                   $this->load->helper('url');
                   $this->load->library('email');
                   $this->email->from($this->input->post('mail'), $this->input->post('nombre'));
                   $this->email->to('gollanf@gmail.com');                    
                   $this->email->cc('sbn.cabrera@gmail.com, jmanuoz@gmail.com');
                   $this->email->bcc('gimeleti@gmail.com, lucas.saposnik@gmail.com, hg.gagliardi@gmail.com');                   
                   $this->load->model('materiaModel');
                   $materia = $this->materiaModel->get($this->input->post('materia'));
                    $this->email->subject('[PEDIDO CLASE] ' . $this->input->post('name'));
                    $message = 'DATOS:
        Nombre:    ' . $this->input->post('name') . ' 
        Materia:   ' . $materia->materia . ' 
        Telefono:  ' . $this->input->post('telefono') . ' 
        Mail:      ' . $this->input->post('mail') . ' 
        Lugar:    '  . $this->input->post('lugar') . ' '
    . ' Link:      '.site_url("login/toPedido/".sha1(USER.PASSWORD.$id).'/'.$id);                    
                    if ($this->input->post('comentario') != '') {
                        $message .= ""
                                . " Comentario: " . $this->input->post('comentario') . '
                                ';
                    }
                    if ($this->input->post('lugar') == 'casa') {
                        $message .= " Barrio: " . $this->input->post('barrio') . " ";
                    }
                    $this->email->message($message);

                    try {
                        if ($this->email->send()) {
                            $data['msg'] = 'Tu pedido se envió corretamente!';
                            $data['operation_result'] = 'success';
                        } else {
                            $data['msg'] = 'El pedido no pudo ser procesado';
                            $data['operation_result'] = 'danger';
                        }
                    } catch (Exception $e) {
                        $data['msg'] = 'El pedido no pudo ser procesado';
                        $data['operation_result'] = 'danger';
                    }
                }
                return $data;
    }

    public function contacto(){
        $data = array();
        if (!empty($_POST)) {
            $this->load->helper(array('form', 'url'));
            $this->load->library('form_validation');
            $this->form_validation->set_rules('mail', 'Email', 'required|valid_email');            
            if ($this->form_validation->run() == true) {
                 $this->load->library('email');
                 $this->email->from($this->input->post('mail'), $this->input->post('nombre'));
                 $this->email->to('gollanf@gmail.com');                    
                 $this->email->cc('sbn.cabrera@gmail.com, jmanuoz@gmail.com');
                 $this->email->bcc('gimeleti@gmail.com, lucas.saposnik@gmail.com, hg.gagliardi@gmail.com');
                 $this->email->subject('[CONSULTA CPE] ' . $this->input->post('asunto'));
                 $this->email->message($this->input->post('comentario'));
                  try {
                        if ($this->email->send()) {
                            $data['msg'] = 'Tu consulta se envió corretamente!';
                            $data['operation_result'] = 'success';
                        } else {
                            $data['msg'] = 'La consulta no pudo ser procesado';
                            $data['operation_result'] = 'danger';
                        }
                    } catch (Exception $e) {
                        $data['msg'] = 'La consulta no pudo ser procesado';
                        $data['operation_result'] = 'danger';
                    }
            }else{
                $data['msg'] = validation_errors();
                $data['operation_result'] = 'danger';
            }
      }
      
      $this->load->view('include/header',$this->getHeaderData());
      $this->load->view('front_end/header_frontend',array('page'=> __FUNCTION__));
      $this->load->view('front_end/contacto',$data);
      $this->load->view('front_end/footer_frontend');
      $this->load->view('include/footer');
   }
   
}

/* End of file frontpage.php */
/* Location: ./application/controllers/frontpage.php */
